package com.braveman.im.ui.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.braveman.im.R;
import com.braveman.im.bean.FriendshipListBean;
import com.braveman.im.bean.GetMineFriendsBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.adapter.PhotoAdapterNew;
import com.braveman.im.ui.adapter.SelectFriendsAdapter;
import com.braveman.im.utils.UtilsStyle;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.widget.TitleBarView;
import com.ht.baselibs.utils.ActivityUtils;
import com.vondear.rxtools.RxBarTool;

import java.util.ArrayList;
import java.util.List;

import ezy.ui.layout.LoadingLayout;
import retrofit2.Call;
import retrofit2.Response;

public class SelectFriendsActivity extends BaseActivity {
    private SelectFriendsAdapter mineFriendsAdapter;
    private List<FriendshipListBean> friendshipListBeanList = new ArrayList<>();//展示数据
    private List<FriendshipListBean> allFriendList = new ArrayList<>();//所有好友
    private RecyclerView mLvActivityListMember;
    private LoadingLayout loadingLayout;
    private List<GetMineFriendsBean> list;
    private ArrayList<String> friendsId = new ArrayList<>();

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("选择联系人").setLeftBtnIcon(R.mipmap.back_black).setOnLeftBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(SelectFriendsActivity.this);
            }
        });
        mTitleBarView.addActions(new TitleBarView.Action(R.color.color_19, "完成") {
            @Override
            public void performAction(View view) {
                for (FriendshipListBean friendshipListBean : friendshipListBeanList) {
                    if (friendshipListBean.isCheck) {
                        friendsId.add(friendshipListBean.friendId);
                    }
                }
                Intent bundle = new Intent();
                if (friendsId != null) {
                    bundle.putStringArrayListExtra("friendsId", friendsId);
                }
                setResult(RESULT_OK, bundle);
                ActivityUtils.finishActivity(SelectFriendsActivity.this);
            }
        });
    }

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_select_friends;
    }

    @Override
    protected void initChildView() {
        mineFriendsAdapter = new SelectFriendsAdapter(R.layout.itme_select_friends, R.layout.item_select_friends_list_head, friendshipListBeanList);
        loadingLayout = findViewById(R.id.loading_layout);
        mLvActivityListMember = findViewById(R.id.lv_Activity_list_member);
        mLvActivityListMember.setLayoutManager(new LinearLayoutManager(SelectFriendsActivity.this));
        mLvActivityListMember.setAdapter(mineFriendsAdapter);
    }

    @Override
    protected void initChildData() {
        ApiManager.getApi().getMineFriendsList().enqueue(new NetCallBack<List<FriendshipListBean>>() {
            @Override
            protected void onSuccess(Call<List<FriendshipListBean>> call, Response<List<FriendshipListBean>> response) {

                if (response != null) {
                    if (friendshipListBeanList != null) {
                        friendshipListBeanList.clear();
                    }
                    if (allFriendList != null) {
                        allFriendList.clear();
                    }
                    allFriendList.addAll(response.body());
                    String name = "";
                    for (FriendshipListBean friendshipListBean : allFriendList) {
                        if (!name.equals(friendshipListBean.userFriendCategoryName)) {
                            FriendshipListBean friendshipHead = new FriendshipListBean(true, friendshipListBean.userFriendCategoryName);
                            friendshipListBeanList.add(friendshipHead);
                        } else {
                            friendshipListBeanList.add(friendshipListBean);
                        }
                        name = friendshipListBean.userFriendCategoryName;
                    }

                    if (friendshipListBeanList.size() == 0) {
                        loadingLayout.showEmpty();
                    } else {
                        loadingLayout.showContent();
                    }
                } else {
                    loadingLayout.showError();
                }
                if (mineFriendsAdapter != null) {
                    mineFriendsAdapter.notifyDataSetChanged();
                }

            }
        });
    }

    @Override
    protected void setChildViewListener() {

    }


}
